package com.syf.core.service.invoke;

import com.syf.core.service.SyService;
import com.syf.core.service.SyServiceDefinition;
import com.syf.core.service.context.ServiceRequest;
import com.syf.core.service.context.ServiceResponse;
import com.syf.core.exception.ServiceNotFoundException;

import java.util.concurrent.Future;

public interface SyServiceInvoker {
    /**
     * 判断service是否存在
     *
     * @param serviceCode 交易码SyfServiceSetting
     */
    boolean isExistService(String serviceCode);

    /**
     * 判断service是否存在
     *
     * @param serviceClass 交易类
     */
    boolean isExistService(Class<? extends SyService> serviceClass);

    /**
     * 获取service definition
     *
     * @param serviceCode 交易码
     */
    SyServiceDefinition getDefinition(String serviceCode);

    /**
     * 获取service definition
     *
     * @param serviceClass 交易类
     */
    SyServiceDefinition getDefinition(Class<? extends SyService> serviceClass);

    /**
     * 构建service request
     */
    ServiceRequest buildServiceRequest(SyServiceDefinition definition, String callingAppComponent, String reqDataJson) throws ServiceNotFoundException;

    /**
     * 根据交易码执行service
     *
     * @param definition service定义
     * @param request    service request 对象
     * @param callback   回调函数
     */
    void invokeService(SyServiceDefinition definition, ServiceRequest request, ServiceInvokeCallback<ServiceResponse> callback) throws ServiceNotFoundException;

    /**
     * 根据交易码执行service
     *
     * @param serviceCode 交易码
     * @param request     service request 对象
     * @param callback    回调函数
     */
    default void invokeService(String serviceCode, ServiceRequest request, ServiceInvokeCallback<ServiceResponse> callback) throws ServiceNotFoundException {
        invokeService(getDefinition(serviceCode), request, callback);
    }

    /**
     * 根据交易码执行service
     *
     * @param serviceCode         交易码
     * @param callingAppComponent 调用service的app component name
     * @param reqDataJson         业务请求数据的json字符串
     * @param callback            回调函数
     */
    default void invokeService(String serviceCode, String callingAppComponent, String reqDataJson, ServiceInvokeCallback<ServiceResponse> callback) throws ServiceNotFoundException {
        SyServiceDefinition definition = getDefinition(serviceCode);
        invokeService(definition, buildServiceRequest(definition, callingAppComponent, reqDataJson), callback);
    }

    /**
     * 根据交易类执行service
     *
     * @param serviceClass 交易的类
     * @param request      service request 对象
     * @param callback     回调函数
     */
    default void invokeService(Class<? extends SyService> serviceClass, ServiceRequest request, ServiceInvokeCallback<ServiceResponse> callback) throws ServiceNotFoundException {
        invokeService(getDefinition(serviceClass), request, callback);
    }

    /**
     * 根据交易类执行service
     *
     * @param serviceClass        交易的类
     * @param callingAppComponent 调用service的app component name
     * @param reqDataJson         业务请求数据的json字符串
     * @param callback            回调函数
     */
    default void invokeService(Class<? extends SyService> serviceClass, String callingAppComponent, String reqDataJson, ServiceInvokeCallback<ServiceResponse> callback) throws ServiceNotFoundException {
        SyServiceDefinition definition = getDefinition(serviceClass);
        invokeService(definition, buildServiceRequest(definition, callingAppComponent, reqDataJson), callback);
    }

    /**
     * 根据交易码执行service
     *
     * @param definition service定义
     * @param request    service request 请求对象
     */
    Future<ServiceResponse> invokeService(SyServiceDefinition definition, ServiceRequest request) throws ServiceNotFoundException;


    /**
     * 根据交易码执行service
     *
     * @param serviceCode 交易码
     * @param request     service request 请求对象
     */
    default Future<ServiceResponse> invokeService(String serviceCode, ServiceRequest request) throws ServiceNotFoundException {
        return invokeService(getDefinition(serviceCode), request);
    }

    /**
     * 根据交易码执行service.
     *
     * @param serviceCode         交易码
     * @param callingAppComponent 调用service的app component name
     * @param reqDataJson         业务请求数据的json字符串
     */
    default Future<ServiceResponse> invokeService(String serviceCode, String callingAppComponent, String reqDataJson) throws ServiceNotFoundException {
        SyServiceDefinition definition = getDefinition(serviceCode);
        return invokeService(definition, buildServiceRequest(definition, callingAppComponent, reqDataJson));
    }

    /**
     * 根据交易类执行service
     *
     * @param serviceClass 交易类
     * @param request      service request 请求对象
     */
    default Future<ServiceResponse> invokeService(Class<? extends SyService> serviceClass, ServiceRequest request) throws ServiceNotFoundException {
        return invokeService(getDefinition(serviceClass), request);
    }

    /**
     * 根据交易类执行service
     *
     * @param serviceClass        交易类
     * @param callingAppComponent 调用service的app component name
     * @param reqDataJson         业务请求数据的json字符串
     */
    default Future<ServiceResponse> invokeService(Class<? extends SyService> serviceClass, String callingAppComponent, String reqDataJson) throws ServiceNotFoundException {
        SyServiceDefinition definition = getDefinition(serviceClass);
        return invokeService(definition, buildServiceRequest(definition, callingAppComponent, reqDataJson));
    }

    /**
     * 在执行线程内执行callback，交易码执行service
     *
     * @param definition service定义
     * @param request    请求对象
     * @param callback   回调
     */
    void asyncInvokeService(SyServiceDefinition definition, ServiceRequest request, ServiceInvokeCallback<ServiceResponse> callback) throws ServiceNotFoundException;

    /**
     * 在执行线程内执行callback，交易码执行service
     *
     * @param serviceCode 交易码
     * @param request     请求对象
     * @param callback    回调
     */
    default void asyncInvokeService(String serviceCode, ServiceRequest request, ServiceInvokeCallback<ServiceResponse> callback) throws ServiceNotFoundException {
        asyncInvokeService(getDefinition(serviceCode), request, callback);
    }

    /**
     * 在执行线程内执行callback，交易码执行service
     *
     * @param serviceCode         交易码
     * @param callingAppComponent 调用service的app component name
     * @param reqDataJson         业务请求数据的json字符串
     * @param callback            回调
     */
    default void asyncInvokeService(String serviceCode, String callingAppComponent, String reqDataJson, ServiceInvokeCallback<ServiceResponse> callback) throws ServiceNotFoundException {
        SyServiceDefinition definition = getDefinition(serviceCode);
        asyncInvokeService(definition, buildServiceRequest(definition, callingAppComponent, reqDataJson), callback);
    }

    /**
     * 在执行线程内执行callback，交易类执行service
     *
     * @param serviceClass 交易类
     * @param request      请求对象
     * @param callback     回调
     */
    default void asyncInvokeService(Class<? extends SyService> serviceClass, ServiceRequest request, ServiceInvokeCallback<ServiceResponse> callback) throws ServiceNotFoundException {
        asyncInvokeService(getDefinition(serviceClass), request, callback);
    }

    /**
     * 在执行线程内执行callback，交易类执行service
     *
     * @param serviceClass        交易类
     * @param callingAppComponent 调用service的app component name
     * @param reqDataJson         业务请求数据的json字符串
     * @param callback            回调
     */
    default void asyncInvokeService(Class<? extends SyService> serviceClass, String callingAppComponent, String reqDataJson, ServiceInvokeCallback<ServiceResponse> callback) throws ServiceNotFoundException {
        SyServiceDefinition definition = getDefinition(serviceClass);
        asyncInvokeService(definition, buildServiceRequest(definition, callingAppComponent, reqDataJson), callback);
    }

    /**
     * 线程池最大线程数
     * */
    Integer getActuatorMaxThreadNum();
}
